package org.forever.util
{
	/**
	* 基于单链表实现队列结构
	*/
	public class Queue_List implements Queue
	{
		protected var head:Node;//指向表首元素
		protected var tail:Node;//指向表末元素
		protected var size:int;//队列中元素的数目
		
		public Queue_List()
		{	head = tail = null;	size = 0; }
		
		public function getSize():int
		{	return size; }
		
		public  function isEmpty():Boolean
		{	return (0 == size) ? true : false; }
		
		public function enqueue( obj:Object):void {
			var node;Node = new Node();
			node.setElem(obj);
			node.setNext(null);//新节点作为末节点插入
			if (0 == size)	head = node;//若此前队列为空，则直接插入
			else tail.setNext(node);//否则，将新节点接至队列末端
			tail = node;//更新指向末节点引用
			size++;//更新规模
		}
		
		public function dequeue():Object {
			if (0 == size)
				throw new Error("意外：队列空");
			var obj:Object = head.getElem();
			head = head.getNext();
			size--;
			if (0 == size)	tail = null;//若队列已空，须将末节点引用置空
			return obj;
		}	
		
		public function front():Object {
			if (isEmpty())
				throw new Error("意外：队列空");
			return head.getElem();
		}
		
		public function traversal():void {
			var p:Node = head;
			while (null != p) {
				trace(p.getElem()+" ");
				p = p.getNext();
			}
		}
	}
}